package com.zhangzhan.gulimallorder.config.mq;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

import static com.zhangzhan.gulimallcommcon.constant.orderAndWareUnLockCons.OrderCancelMqConstant.*;
import static com.zhangzhan.gulimallcommcon.constant.orderAndWareUnLockCons.WareUnLockConstant.WARE_RELEASE_WARE;
import static com.zhangzhan.gulimallcommcon.constant.orderAndWareUnLockCons.WareUnLockConstant.WARE_RELEASE_WARE_QUEUE;


/**
 * @author 张一恒
 * @version 1.0
 * @description: 订单业务异常或超时库存解锁配置类 ,spring把mq配置加入到ioc中会自动创建对应的队列或交换机也会自动绑定。只有一次效果，第二次重启服务不会覆盖相关交换机或队列之前配置。
 * @date 2024/7/2 11:28
 */
@Configuration
public class OrderWareUnlockMqConfig {

    /**
     * 延时队列
     *
     * @return
     */
    @Bean
    public Queue orderCreateOrder() {
        Map<String, Object> arguments = Map.of(X_DEAD_LETTER_EXCHANGE, ORDER_EVENT_EXCHANGE,
                X_DEAD_LETTER_ROUTING_KEY, ORDER_RELEASE_ORDER,
                X_MESSAGE_TTL, ORDER_FAIL_TTL);
        return new Queue(ORDER_DELAY_QUEUE, true, false, false, arguments);
    }


    /**
     * 接收死信的队列
     *
     * @return
     */
    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue(ORDER_RELEASE_ORDER_QUEUE, true, false, false);
    }

    /**
     * 处理和接收死信的交换机
     *
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(ORDER_EVENT_EXCHANGE, true, false);
    }


    /**
     * 延时队列绑定交换机
     *
     * @return
     */
    @Bean
    public Binding eventExchangeBindingOrderCreate() {
        return new Binding(ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE, ORDER_CREATE_ORDER, null);
    }

    /**
     * 死信队列绑定交换机
     *
     * @return
     */
    @Bean
    public Binding eventExchangeBindingOrderRelease() {
        return new Binding(ORDER_RELEASE_ORDER_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE, ORDER_RELEASE_ORDER, null);
    }

    /**
     * 订单取消成功主动提醒解锁库存死信队列进行解锁
     *
     * @return
     */
    @Bean
    public Binding eventExchangeBindingWareRelease() {
        return new Binding(WARE_RELEASE_WARE_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE, WARE_RELEASE_WARE, null);
    }

    /**
     * 秒杀创建订单队列
     *
     * @return
     */
    @Bean
    public Queue seckillCreateOrderQueue() {
        return new Queue(SECKILL_CREATE_ORDER_QUEUE, true, false, false);
    }


    /**
     * 秒杀创建订单。绑定秒杀队列
     *
     * @return
     */
    @Bean
    public Binding eventExchangeBindingSeckill() {
        return new Binding(SECKILL_CREATE_ORDER_QUEUE, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE, SECKILL_CREATE_ORDER, null);
    }


}
